package java_lambda;

import java.util.*;
import java.util.stream.Collectors;

/**
 * TestLambda
 *
 * @Description：Lambda十种运用方式
 * @author: 清风
 * @date: 2023/12/13
 */
public class TestLambda {

    public static void main(String[] args) {

        List<String> list = Arrays.asList("apple","banana","orange");
        //传统的循环输出方式
        for (String s : list) {
            System.out.println("传统的循环输出方式："+s);
        }
//
//        //循环遍历中的第一种使用（只有一行时，可以这么使用）
//        list.forEach(System.out::println);
//
//        //第二种使用
//        list.forEach(s -> System.out.println("Lambda表达1："+s));

        // 排序使用 (传统)
        Collections.sort(list, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });
        //改为Lambda表达
        Collections.sort(list,(o1,o2) -> o1.compareTo(o2));


        //场景三：取出a开头的数据
        List<String> list2 = new ArrayList<String>();
        for (String s:list) {
            if (s.startsWith("a")){
                list2.add(s);
            }
        }
        //改为Lambda表达
        List<String> list3 = list.stream().filter(s -> s.startsWith("a")).collect(Collectors.toList());

        //场景四：映射，获取长度
        List<Integer> list4 = new ArrayList<>();
        for (String s:list) {
            list4.add(s.length());
        }
        //改为Lambda表达，.map（）对list中的每一个元素做了转换处理，等同于String 转为了Integer
        List<Integer> list5 = list.stream().map(s -> s.length()).collect(Collectors.toList());


        //场景五：规约
        List<Integer> list6 = Arrays.asList(1,2,3,4,5);
        int sum = 0;
        for (Integer i :list6) {
            sum +=i;
        }
        System.out.println(sum);

        //改为Lambda表达
        int sum2 = list6.stream().reduce(0,(a,b) -> a+b);
        System.out.println(sum2);


        //场景六：分组
        Map<Integer,List<String>> groups = new HashMap<>();
        for (String s: list) {
            int length = s.length();
            if (!groups.containsKey(length)){
                groups.put(length,new ArrayList<>());
            }
            groups.get(length).add(s);
        }
        System.out.println(groups);

        //改为Lambda表达分组
        Map<Integer,List<String>> groups2=list.stream().collect(Collectors.groupingBy(String::length));
        System.out.println(groups2);


        //场景七：函数式接口实现
        MyInterface myInterface = new MyInterface() {
            @Override
            public void doSomething(String s) {
                System.out.println(s);
            }
        };

        myInterface.doSomething("hello,world");

        //改为Lambda表达
        MyInterface myInterface1 = (s -> System.out.println(s));
        myInterface1.doSomething("hello,world");

        //场景8：创建线程
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("hello,thread");
            }
        });

        thread.start();

        Thread thread1 = new Thread(() -> System.out.println("hello,thread1"));

        thread1.start();


        //场景9：Option操作
        String ste ="hello,world";
        if (ste!=null){
            System.out.println(ste.toUpperCase());
        }
        //改为Lambda表达
        Optional.ofNullable(ste).map(String::toUpperCase).ifPresent(System.out::println);


        //场景9，流水线
        List<String> list7 = new ArrayList<>();
        for (String s: list) {
            if (s.startsWith("a")){
                list7.add(s.toUpperCase());
            }
        }


        List<String> list8 = list.stream().filter(s -> s.startsWith("a")).map(String::toUpperCase).sorted().collect(Collectors.toList());

        System.out.println(list7);
        System.out.println(list8);

    }
}
